import 'dart:io';

import 'package:dio/dio.dart';
import 'package:sainbainu/common/apis/common_api.dart';
import 'package:sainbainu/common/enums/search_type.dart';
import 'package:sainbainu/common/models/request/sms_request.dart';
import 'package:sainbainu/common/models/response/area_list_response.dart';
import 'package:sainbainu/common/models/response/oss_auth_response.dart';
import 'package:sainbainu/common/models/response/upload_response.dart';

class CommonRepository {
  final CommonApi _commonApi;

  CommonRepository(this._commonApi);

  /// 上传文件
  Future<UploadResponse> upload(
    File file, {
    ProgressCallback? onSendProgress,
    ProgressCallback? onReceiveProgress,
  }) async {
    var res = await _commonApi.upload(
      file,
      onSendProgress: onSendProgress,
      onReceiveProgress: onReceiveProgress,
    );
    return UploadResponse.fromJson(res);
  }

  /// 发送短信验证码
  Future<Map<String, dynamic>> sendSms(SmsRequest smsRequest) async {
    var res = await _commonApi.sendSms(smsRequest.toJson());
    return res;
  }

  /// 获取地区列表
  Future<AreaListResponse> areaList(String pid) async {
    var res = await _commonApi.areaList({'pid': pid});
    return AreaListResponse.fromJson(res);
  }

  /// 删除文件
  Future<Map<String, dynamic>> delFile(List<String> keyNames) async {
    var res = await _commonApi.delFile({'keyNames': keyNames});
    return res;
  }

  /// 删除搜索历史
  Future<Map<String, dynamic>> clearSearchHistory() async {
    var res = await _commonApi.clearSearchHistory();
    return res;
  }

  /// 获取搜索历史
  Future<List<String>> searchHistory() async {
    var res = await _commonApi.searchHistory();
    return (res['data'] as List).map((e) => e as String).toList();
  }

  /// 获取热门搜索
  Future<List<String>> searchHot() async {
    var res = await _commonApi.searchHot();
    return (res['data'] as List).map((e) => e as String).toList();
  }

  /// 搜索
  Future<Map<String, dynamic>> search({
    required String content,
    required SearchType type,
  }) async {
    var res = await _commonApi.search({
      'content': content,
      'type': type.value,
    });
    return res;
  }

  /// 获取 oss -> sts
  Future<OssAuthResponse> getAuthSts() async {
    var res = await _commonApi.getAuthSts();
    return OssAuthResponse.fromJson(res['data']);
  }
}
